package per.lee.spingboothelper.db.controller;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import per.lee.spingboothelper.db.converter.DescConvertColumn;
import per.lee.spingboothelper.db.model.Column;

@RestController
@RequestMapping("/selectSql")
public class SelectSqlController {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	private static Set<String> filter_column = new HashSet<String>();
	
	static {
		filter_column.add("subject_type");
		filter_column.add("change_type");
	}
			
	@RequestMapping("/total")
	public String mockData(@RequestParam("table") String table) {
	
		List<Map<String, Object>> queryForList = jdbcTemplate.queryForList("desc " + table);

		List<Column> columns = DescConvertColumn.convert(queryForList);

		StringBuffer sb = new StringBuffer("select ");
	
		for(Column c:columns) {
			if(filter_column.contains(c.getName())) {
				sb.append(c.getName()+",");
			}else {
				sb.append("sum("+c.getName()+") as "+c.getName()+",");
			}
		}
		String start = sb.substring(0,sb.length()-1);
		return start + " from "+table+" where ";
	}
	

}